/**************************************************************
 *@ProjectName: blweibo
 *@FileName: TopicUtil.java
 *@Date: 2011-5-30
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo.util;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.Spannable;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;

import com.zhiweibo.EditActivity;
import com.zhiweibo.EmotionView;

/**
 * The Class TopicUtil.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-30
 */
public class TopicUtil {
    
    /** The atpattern. */
    private static Pattern atpattern;
    
    /** The sharppattern. */
    private static Pattern sharppattern;
    
    /** The webpattern. */
    private static Pattern webpattern;
    
    /** The impressionpattern. */
    private static Pattern impressionpattern;

    /**
     * Gets the punctuation.
     * 
     * @return the punctuation
     */
    private static String getPunctuation() {
        return "`~!@#\\$%\\^&*()=+\\[\\]{}\\|/\\?<>,\\.:\\u00D7\\u00B7\\u2014-\\u2026\\u3001-\\u3011\\uFE30-\\uFFE5";
    }

    /**
     * Gets the at pattern.
     * 
     * @return the at pattern
     */
    public static Pattern getAtPattern() {
        if (atpattern == null) {
            String str = getPunctuation();
            atpattern = Pattern.compile(String.format("@[[^@\\s%s]0-9]{1,20}", str));
        }
        return atpattern;
    }

    /**
     * Gets the sharp pattern.
     * 
     * @return the sharp pattern
     */
    private static Pattern getSharpPattern() {
        if (sharppattern == null) {
            sharppattern = Pattern.compile("#[^#]+?#");
        }
        return sharppattern;
    }

    /**
     * Gets the impressionpattern.
     * 
     * @return the impressionpattern
     */
    public static Pattern getImpressionpattern() {
        if (impressionpattern == null) {
            impressionpattern = Pattern.compile("[TI]_ADD.*_[TI]_ADD");
        }
        return impressionpattern;
    }

    /**
     * Gets the web pattern.
     * 
     * @return the web pattern
     */
    public static Pattern getWebPattern() {
        if (webpattern == null) {
            webpattern = Pattern
                    .compile("http[s]*://[[[^/:]&&[a-zA-Z_0-9]]\\.]+(:\\d+)?(/[a-zA-Z_0-9]+)*(/[a-zA-Z_0-9]*([a-zA-Z_0-9]+\\.[a-zA-Z_0-9]+)*)?(\\?(&?[a-zA-Z_0-9]+=[%[a-zA-Z_0-9]-]*)*)*(#[[a-zA-Z_0-9]|-]+)?");
        }
        return webpattern;
    }

    /**
     * Format date.
     * 
     * @param strDate
     *            the str date
     * @return the string
     */
    public static String formatDate(String strDate) {
        Calendar now = Calendar.getInstance();
        long l2 = now.getTimeInMillis();
        long l3 = Long.parseLong(strDate) * 1000;
        if (l2 - l3 < 60000) {
            return "刚刚";
        } else if (l2 - l3 < 3600000) {
            return (l2 - l3) / 60000 + "分钟前";
        } else {
            String temp1 = MessageFormat.format("{0,date,yyyy-MM-dd}", l2);
            String temp2 = MessageFormat.format("{0,date,yyyy-MM-dd}", l3);
            if (temp1.equals(temp2)) {
                return "今天" + MessageFormat.format("{0,date,HH:mm}", l3);
            } else {
                return MessageFormat.format("{0,date,MM-dd HH:mm}", l3);
            }
        }
    }

    /**
     * Format from.
     * 
     * @param str
     *            the str
     * @return the string
     */
    public static String formatFrom(String str) {
        Matcher localMatcher = Pattern.compile("<a.*>(.*)</a>").matcher(str);
        if (localMatcher.find()) {
            return "来自：" + localMatcher.group(1);
        }
        return str;
    }

    /**
     * Highlight content.
     * 
     * @param paramContext
     *            the param context
     * @param paramSpannable
     *            the param spannable
     * @param b
     *            the b
     */
    public static void highlightContent(Context paramContext, Spannable paramSpannable, boolean b) {
        int i = 2;
        int j = Color.BLUE;
        int k = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
        int m = 0;
        int n = 0;
        Matcher localMatcher = getAtPattern().matcher(paramSpannable);
        while (localMatcher.find()) {
            m = localMatcher.start();
            n = localMatcher.end();
            if (n - m == i) {
                int i1 = m + 1;
                if (paramSpannable.charAt(i1) != '我') {
                    continue;
                }
            } else {
                if (n - m <= i) {
                    continue;
                }
            }
            ForegroundColorSpan localForegroundColorSpan1 = new ForegroundColorSpan(j);
            paramSpannable.setSpan(localForegroundColorSpan1, m, n, k);
            if (b) {
                String str1 = paramSpannable.subSequence(m, n).toString();
                AtClicker localAtClicker = new AtClicker(paramContext, str1 + ":");
                paramSpannable.setSpan(localAtClicker, m, n, k);
            }
        }
        localMatcher = getWebPattern().matcher(paramSpannable);
        while (localMatcher.find()) {
            ForegroundColorSpan localForegroundColorSpan2 = new ForegroundColorSpan(j);
            m = localMatcher.start();
            n = localMatcher.end();
            paramSpannable.setSpan(localForegroundColorSpan2, m, n, k);
            if (b) {
                UrlClicker localUrlClicker = new UrlClicker(paramContext, paramSpannable.subSequence(m, n).toString());
                paramSpannable.setSpan(localUrlClicker, m, n, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        localMatcher = getSharpPattern().matcher(paramSpannable);
        while (localMatcher.find()) {
            ForegroundColorSpan localForegroundColorSpan3 = new ForegroundColorSpan(j);
            m = localMatcher.start();
            n = localMatcher.end();
            paramSpannable.setSpan(localForegroundColorSpan3, m, n, k);
            if (b) {
                String str1 = paramSpannable.subSequence(m, n).toString();
                AtClicker localAtClicker = new AtClicker(paramContext, str1);
                paramSpannable.setSpan(localAtClicker, m, n, k);
            }
        }
        localMatcher = Pattern.compile("\\[([^]]+?)\\]").matcher(paramSpannable);
        while (localMatcher.find()) {
            HashMap localHashMap = EmotionView.emotionsKeyString;
            String str = localMatcher.group(1);
            Integer localInteger = (Integer) localHashMap.get(str);
            if ((localInteger != null) && (localInteger.intValue() > 0)) {
                int i6 = localMatcher.start();
                int i7 = localMatcher.end();
                int i8 = localInteger.intValue();
                ImageSpan localImageSpan = new ImageSpan(paramContext, i8);
                paramSpannable.setSpan(localImageSpan, i6, i7, k);
            }
        }
    }

    /**
     * The Class AtClicker.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-30
     */
    static class AtClicker extends ClickableSpan {
        
        /** The nick name. */
        private final String nickName;
        
        /** The m context. */
        private Context mContext;

        /**
         * Instantiates a new at clicker.
         * 
         * @param context
         *            the context
         * @param arg2
         *            the arg2
         */
        public AtClicker(Context context, String arg2) {
            nickName = arg2;
            mContext = context;
        }

        /* (non-Javadoc)
         * @see android.text.style.ClickableSpan#onClick(android.view.View)
         */
        @Override
        public void onClick(View paramView) {
            Intent localIntent = new Intent(mContext, EditActivity.class);
            localIntent.putExtra("mode", 0);
            localIntent.putExtra("content", nickName);
            mContext.startActivity(localIntent);
        }
    }

    /**
     * The Class UrlClicker.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-30
     */
    static class UrlClicker extends ClickableSpan {
        
        /** The url path. */
        private final String urlPath;
        
        /** The m context. */
        private Context mContext;

        /**
         * Instantiates a new url clicker.
         * 
         * @param context
         *            the context
         * @param paramString1
         *            the param string1
         */
        public UrlClicker(Context context, String paramString1) {
            mContext = context;
            urlPath = paramString1;
        }

        /* (non-Javadoc)
         * @see android.text.style.ClickableSpan#onClick(android.view.View)
         */
        public void onClick(View paramView) {
            mContext.startActivity(new Intent("android.intent.action.VIEW").setData(Uri.parse(urlPath)));
        }
    }

}
